// THIS IS A BACKUP MADE AFTER TRYING TO CREATE 2 SEPARATE SOUND OBJECTS TO MAKE IT WORK

/* package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;

	public class FadingSound
	{
		public static var MIN_PLAY_TIME : int = 1500;
		
 		public var isSoundPlaying:Boolean = false;
 		public var volume:Number = 1;
 		public var fadeInVolume:Number = 0;
 		// MAKE STATIC
 		public var delay:Number = 5000;
 		
 		private var _fadeInChannel:SoundChannel;
 		private var fadeOutChannel:SoundChannel;
 		private var currentSound:Sound = new Sound();;
 		private var _currentChannel:SoundChannel = new SoundChannel();
 		private var playlist:ArrayCollection;
 		private var curTrack:Number = 0; // index of currently playing track in playlist
 		private var currentURL:String;
 		private var streamQueue:ArrayCollection=new ArrayCollection(new Array());
 		private var startTime:Number;
		private var isFading:Boolean = false;
		private var isFadingIn:Boolean = true;
		private var numTracksStoppedAfterFadeOut : Number = 0;
		private var fadeOutTrack : Number = 0; // used to keep track of which track in the playlist is currently fading out.
		// due to the nature of crossfading, there can be discrepancies between this var and curTrack
		
		private var sound1 : Sound = new Sound();
		private var sound2 : Sound = new Sound();
		[Bindable] public var sc1:SoundChannel = new SoundChannel();
		[Bindable] public var sc2:SoundChannel = new SoundChannel();
 	
 	// TRY TO USE FRAME EVENT, IF NOT, USE TIMER
 		private var timer:Timer = new Timer(50);
	
		// CURRENT CHANNEL
		private function set currentChannel(sc:SoundChannel):void
		{
			_currentChannel = sc;
		}
		private function get currentChannel():SoundChannel
		{
			return _currentChannel;
		}
		
		// FADE IN CHANNEL
		private function set fadeInChannel(sc:SoundChannel):void
		{
			_fadeInChannel = sc;
		}
		private function get fadeInChannel():SoundChannel
		{
			return _fadeInChannel;
		}
		
		public function StartPlaylistTEST(tracks:ArrayCollection, fadeOutLength:int=-1, useDefault:Boolean=true):void
		{
			currentSound = new Sound();
					currentChannel=new SoundChannel();

					//add handler for ioerrors:
					currentSound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true );
				
				//	addEventListener( Event.ENTER_FRAME, onEnterFrame, false, 0, true );
					
					//load sound:
					var urlReq:URLRequest = new URLRequest("64mono.mp3");
					currentSound.load(urlReq);
					
					currentChannel = currentSound.play(); //play sound:
					
					fadeInChannel = currentChannel;
					
			var fadeOut:SoundTransform = new SoundTransform;
			fadeOut.pan = -1
			currentChannel.soundTransform = fadeOut;	 
			
			var fadeOut2:SoundTransform = new SoundTransform;
			fadeOut2.pan = 1;
			fadeInChannel.soundTransform = fadeOut2;
					
		}
		
		public function changeVolumeCh1(vol:Number):void
		{
			changeVolume(vol, currentChannel,-1);
		}
		
		public function changeVolumeCh2(vol:Number):void
		{
			changeVolume(vol, fadeInChannel, 1);
		}
		
		public function StartPlaylist(tracks:ArrayCollection, fadeOutLength:int=-1, useDefault:Boolean=true):void
		{
			
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			SoundMixer.stopAll();
			//clearTimers();

				//reset track count
				curTrack=0;
				numTracksStoppedAfterFadeOut=0;
				isFading = false;
				playlist = tracks;
				PlayNextSong();
				fadeOutChannel = currentChannel;
/* 			else
			{
				StopPlaylist();
			} */
		}
		
		///////////////////////
		// PlayNextSong
		///////////////////////
		private function PlayNextSong():void
		{	
			// if this isn't the first track in playlist && AlbumTracks has been set
			if (curTrack != 0 )
			{

			}
			else if( curTrack == 0  ) 
			{
			}
			
			if (curTrack < playlist.length)
			{
				currentURL = playlist[curTrack];
				
				if (currentURL == null)
				{
					// since the URL is null, update the count and play the next song
					curTrack++;
					PlayNextSong();
				} 
				else
				{
					// set volume for fade in
					fadeInVolume = 0;
					// reset fadeInChannel
					fadeInChannel = new SoundChannel();
					
					// In order to fade in/out, there needs to be 2 unique sound channels
					// Use a different sound depending on what track we're on now. 
					if ( curTrack % 2 == 0 )
					{ 
						//fadeInChannel = startSound(sound1, fadeInChannel);
						//  use sound1
						sc1 = startSound(sound1, sc1);
						changeVolume(fadeInVolume, sc1);
 					}
					else
					{
						// Sound1 is probably already being used, use Sound2
						sc2 = startSound(sound2, sc2);
						changeVolume(fadeInVolume, sc2);
					}
	
					isFadingIn = true;
					isSoundPlaying = true;
					
// now we should do some update to say which track is playing
					
					// update count
					curTrack++;
				}
			}
			else
			{
			//	StopPlaylist();
			}
			
		}
		
		private function startSound(sound:Sound, channel:SoundChannel):SoundChannel
		{
			// FIND A WAY TO NOT DO THIS HERE - IT FUCKS UP ANY SIMBLANCE OF OOP
				if ( curTrack % 2 == 0 )
				{
					sound1 = new Sound();
					sound = sound1;
					trace("==========sound1===========");
				}
				else
				{
					sound2 = new Sound();
					sound = sound2;
					trace("**********sound2**********");
				}
					channel = new SoundChannel();

					//add handler for ioerrors:
					//currentSound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true );
				
					timer.start();
					
					//load sound:
					var urlReq:URLRequest = new URLRequest(currentURL);
					sound.load(urlReq);
					
					channel = sound.play(); //play sound:
					
					channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
					streamQueue.addItem(channel);	
					
					return channel;
		}

		//public function onEnterFrame(delay:Number):void
		private function onTimer(event:TimerEvent):void
		{		
			// delay = how long until fade out starts
			// checkf or previews that are shorter than specified play length			
			//MIN_PLAY_TIME = default minimum play time
			if (delay > currentSound.length && currentSound.length > MIN_PLAY_TIME )
			{
				// the length is not correctly calculated until the song is fully loaded
				// which causes the player to think the song has already reached the delay time
				// and the player goes to the next song but also keeps playing the first one
				if ( currentSound.bytesLoaded < currentSound.bytesTotal )
				{
					// song is not fully loaded so ignore
					
				}
				else
				{
					// song is fully loaded and is shorter than the typical sample length
					delay = currentSound.length - MIN_PLAY_TIME;
				}
			} 
			
			//////
			// FADE IN
			////
			
			if ( isFadingIn) 
				FadeIn();
				
			if ( currentChannel.position >= delay && !isFadingIn)
			{
				isFading = true;
			}
			// if
			if ( isFading) 
				FadeOut();
			// output some values:
			trace("sc1 volume = " + sc1.soundTransform.volume);
			trace("sc2 volume = " + sc2.soundTransform.volume);
		}
			  	// Changes the current volume to "value"
	  	public function changeVolume(value:Number, channel:SoundChannel, pan:Number=0):void
	  	{
			var fadeOut:SoundTransform = new SoundTransform;
			fadeOut.volume = value;
			fadeOut.pan = pan;
			channel.soundTransform = fadeOut;	  	
	  	}
	  	
	  	public function StopPlaylist():void
	  	{
	  		currentChannel.stop();
					isSoundPlaying = false;
					//CommonModelLocator.getInstance().stopFrameEvents = true;
	  	}

		private function FadeOut():void
		{
			try
			{
				if ( volume == 1 )
				{
					// save the number of the track we're fading out
					fadeOutTrack = curTrack;
					
					var ch:SoundChannel;
					var s:Sound;
					// NOTE DIFFERENT COMPARISON == 1
					if ( fadeOutTrack % 2 ==  1 )
					{
						ch = sc1;
						s = sound1;
						trace("Fading Out Sound1");
					}
					else
					{
						ch = sc2;
						s = sound2;
						trace("Fading Out Sound2");
					}
					//fadeOutChannel = currentChannel;
				//	currentChannel = new SoundChannel();
					// turn volume down to prevent clipping
					volume -= .1	
					changeVolume(volume, ch);	
					PlayNextSong();
				}
					
				else if (0 < volume && volume< 1 )
				{
					var ch:SoundChannel;
					var s:Sound;
					// NOTE DIFFERENT COMPARISON == 1
					if ( fadeOutTrack % 2 ==  1 )
					{
						ch = sc1;
						s = sound1;
						trace("Fading Out Sound1");
					}
					else
					{
						ch = sc2;
						s = sound2;
						trace("Fading Out Sound2");
					}
					volume -= .025;
					changeVolume(volume, ch);
				}
				else if ( volume<= 0) 
				{
					var ch:SoundChannel;
					var s:Sound;
					// NOTE DIFFERENT COMPARISON == 1
					if ( fadeOutTrack % 2 ==  1 )
					{
						ch = sc1;
						s = sound1;
						trace("Fading Out Sound1");
					}
					else
					{
						ch = sc2;
						s = sound2;
						trace("Fading Out Sound2");
					}
					ch.stop();
					s.close();
					
					numTracksStoppedAfterFadeOut++;
					if (streamQueue.length>0) 
						streamQueue.removeItemAt(0);
						
					volume = 1;
					isFading = false;
					
					// if we're just using this for 1 song, then we can stop the timer
					// TODO: stop timer on SoundComplete Event
					
					timer.stop();
				
				// stop the current channel from playing	
				// MAYBE?		StopPlaylist();
				
					}
			}
			catch (err:Error)
			{
				trace("error in FadeOut");
				//m.stopFrameEvents=true;
			}
		}
  	
  		private function FadeIn():void
		{
			try
			{
				var ch:SoundChannel;
				var s:Sound;
				// this is different than the other check because curTrack has changed!!
				if ( curTrack % 2 == 1 )
				{
					ch = sc1;
					s = sound1;
					trace("Fading IN Sound1");
				}
				else
				{
					ch = sc2;
					s = sound2;
					trace("Fading IN Sound2");
				}
				if ( fadeInVolume == 0 && ch)
				{
					//fadeInChannel = currentChannel;
					fadeInVolume = .025;
					// turn volume down to prevent clipping
					changeVolume(fadeInVolume, ch);	
				//	PlayNextSong();
				}
					
				else if (0 <= fadeInVolume && fadeInVolume< 1 )
				{
					fadeInVolume += .025;
					changeVolume(fadeInVolume, ch);
				}
				else if ( fadeInVolume>= 1) 
				{
					numTracksStoppedAfterFadeOut++;
					if (streamQueue.length>0) 
						streamQueue.removeItemAt(0);
						
					fadeInVolume = 1;
					volume = 1;
					isFadingIn = false;
					// now that we are no longer fading in, assign this channel to currentChannel
					currentChannel = ch;
					// wash our hands of this
				
				// MAYBE?		StopPlaylist();
					}
			}
			catch (err:Error)
			{
				trace("error in FadeOut");
				//m.stopFrameEvents=true;
			}
		}
		
  		private function ioErrorHandler( evt:IOErrorEvent ):void
		{
			//Alert.show(evt.toString());
			PlayNextSong();
		}
		
		private function onSoundComplete( event:Event)
		{
			trace("onSoundComplete");
		}
	}
	
} */